package com.third.device.mg.serialport.factory.oupaike

import android.util.Log
import com.third.device.mg.serialport.utils.to2ByteL
import com.third.device.mg.serialport.utils.toHexString
import com.third.device.mg.serialport.utils.toIntU

/**
 * @Date 2022/3/2
 * Description:
 * 从帧头开始按字节求和得出的结果, 低字节在前
 */
object OuPaiKeCRC {

    /**
     * 校验发送的指令
     */
    fun commandCheck(msg:ByteArray):ByteArray{

        var sum = 0
        msg.forEach {
            sum += it.toIntU()
        }
        val check = sum.to2ByteL()
        val result = ByteArray(msg.size + check.size)
        System.arraycopy(msg ,0 ,result ,0 ,msg.size)
        System.arraycopy(check ,0 ,result ,msg.size ,check.size)
        return result
    }
}